package com.ftg.learn.pattern.proxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author kangjx
 * @createTime 2022/8/10
 */
public class Play {

    public static void main(String[] args) {
//        Isinger a = new SingerImpl();
//        a.setName("孟德华");
//        a.song();
//        a.dance();
//
//        Isinger ab = new SingerProxy(a);
//        ab.song();
        //代理模式  动态
        SingerImpl s = new SingerImpl();
        s.setName("孟德华");
        Isinger ac = (Isinger) Proxy.newProxyInstance(
                Isinger.class.getClassLoader(),
                new Class[]{Isinger.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if("song".equals(method.getName())){
                            System.out.println("孟德华唱歌");
                        }else if("dance".equals(method.getName())){
                            System.out.println("孟德华跳舞");
                        }
                        System.out.println("---->大家 能看到我不!!!");
                        method.invoke(s,args);
                        System.out.println("-----?我是后面的");
                        return null;
                    }
                });
        ac.song();
        ac.dance();

        Isinger ac1 = (Isinger)Proxy.newProxyInstance(Isinger.class.getClassLoader(),null,(p,m,a)->{
            if("song".equals(m.getName())){
                System.out.println("孟德华唱歌");
            }else if("dance".equals(m.getName())){
                System.out.println("孟德华跳舞");
            }
            System.out.println("---->大家 能看到我不!!!");
            m.invoke(s,args);
            System.out.println("-----?我是后面的");
            return null;
        });
    }

}
